#include <wx/defs.h>
#include <wx/radiobox.h>
#include <wx/window.h>
#include "wxsradiobox.h"

namespace {
  wxsRegisterItem<wxsRadioBox> Reg( _T( "RadioBox" ), _T( "单选分组框" ), wxsTWidget, _T( "标准" ), 180 );
  WXS_ST_BEGIN( wxsRadioBoxStyles, _T( "" ) )
  WXS_ST_CATEGORY( "wxRadioBox" )
  WXS_ST( wxRA_SPECIFY_COLS )
  WXS_ST( wxRA_HORIZONTAL )
  WXS_ST( wxRA_SPECIFY_ROWS )
  WXS_ST( wxRA_VERTICAL )
  WXS_ST_DEFAULTS()
  WXS_ST_END()
  WXS_EV_BEGIN( wxsRadioBoxEvents )
  WXS_EVI( EVT_RADIOBOX, wxEVT_COMMAND_RADIOBOX_SELECTED, wxCommandEvent, Select )
  WXS_EV_END()
}

wxsRadioBox::wxsRadioBox( wxsItemResData* Data ):
  wxsWidget( Data, &Reg.Info, wxsRadioBoxEvents, wxsRadioBoxStyles ), Label( _T( "Label" ) ), DefaultSelection( -1 ), Dimension( 1 ) {}

void wxsRadioBox::OnBuildCreatingCode() {
  switch( GetLanguage() ) {
    case wxsCPP: {
      AddHeader( _T( "<wx/radiobox.h>" ), GetInfo().ClassName, hfInPCH );
      wxString ChoicesName;
      if( ArrayChoices.GetCount() > 0 ) {
        ChoicesName = GetCoderContext()->GetUniqueName( _T( "__wxRadioBoxChoices" ) );
        Codef( _T( "wxString %s[%d] = \n{\n" ), ChoicesName.wx_str(), ( int )ArrayChoices.GetCount() );
        for( size_t i = 0; i < ArrayChoices.GetCount(); ++i ) {
          Codef( _T( "\t%t%s\n" ), ArrayChoices[i].wx_str(), ( ( i != ArrayChoices.GetCount() - 1 ) ? _T( "," ) : _T( "" ) ) );
        }
        Codef( _T( "};\n" ) );
      }
      if( Dimension < 1 ) {
        Dimension = 1;
      }
      Codef( _T( "%C(%W, %I, %t, %P, %S, %d, %s, %d, %T, %V, %N);\n" ),
             Label.wx_str(), ArrayChoices.GetCount(),
             ( ArrayChoices.IsEmpty() ? _T( "0" ) : ChoicesName.wx_str() ),
             Dimension );
      if( DefaultSelection >= 0 && DefaultSelection < ( int )ArrayChoices.GetCount() ) {
        Codef( _T( "%ASetSelection(%d);\n" ), DefaultSelection );
      }
      BuildSetupWindowCode();
      return;
    }
    case wxsUnknownLanguage:
    default: {
      wxsCodeMarks::Unknown( _T( "wxsRadioBox::OnBuildCreatingCode" ), GetLanguage() );
    }
  }
}

wxObject* wxsRadioBox::OnBuildPreview( wxWindow* Parent, long Flags ) {
  wxRadioBox* Preview = new wxRadioBox( Parent, GetId(), Label, Pos( Parent ), Size( Parent ), ArrayChoices, Dimension, Style() );
  if( DefaultSelection >= 0 && DefaultSelection < ( int )ArrayChoices.GetCount() ) {
    Preview->SetSelection( DefaultSelection );
  }
  return SetupWindow( Preview, Flags );
}

void wxsRadioBox::OnEnumWidgetProperties( M_Code_unused long Flags ) {
  WXS_SHORT_STRING( wxsRadioBox, Label, _T( "Label" ), _T( "label" ), _T( "" ), true )
  WXS_ARRAYSTRING( wxsRadioBox, ArrayChoices, _T( "Choices" ), _T( "content" ), _T( "item" ) )
  WXS_LONG( wxsRadioBox, DefaultSelection, _T( "Default" ), _T( "default" ), 0 )
  WXS_LONG( wxsRadioBox, Dimension, _T( "Dimension" ), _T( "dimension" ), 1 )
}
